var loginName = getCookie("loginName");
var password = getCookie("password");
$("#LAY-user-login-username").val(loginName);
$("#LAY-user-login-password").val(password);
if (loginName!=''&&password!=''){
    $("#ck").attr({checked:true});
}
layui.config({
    base: '' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'user'], function(){
    var $ = layui.$
        ,setter = layui.setter
        ,admin = layui.admin
        ,form = layui.form
        ,router = layui.router()
        ,search = router.search;
    form.render();
    //提交
    form.on('submit(LAY-user-login-submit)', function(obj){
        //请求登入接口
        admin.req({
            url: 'htz/user/login' //实际使用请改成服务端真实接口
            ,data: {loginName:obj.field.username,password: obj.field.password}
            ,statusCode:{
                404:function () {
                    window.location.href='404.html'
                }
            }
            ,done: function(res){
                //请求成功后，写入 access_token
                // layui.data(setter.tableName, {
                //     key: setter.request.tokenName
                //     ,value: res.data.access_token
                // });
                if (obj.field.remember){
                    setCookie("loginName",$("#LAY-user-login-username").val(),60*24*30)
                    setCookie("password",$("#LAY-user-login-password").val(),60*24*30)
                }else {
                    setCookie("loginName","",0)
                    setCookie("password","",0)
                }
                if (res.obj!=null){
                    setCookie("token",res.obj,120);
                    //登入成功的提示与跳转
                    layer.msg('登入成功', {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1000
                    }, function(){
                        location.href = 'index.html'; //后台主页
                    });
                }else {
                    layer.msg(res.msg);
                }
            }
        });
    });
    layui.$("#LAY-user-login-username").blur(function () {
        if ($("#LAY-user-login-username").val()!=''){
            $.post("htz/user/loginName",{loginName:$("#LAY-user-login-username").val()},function (res) {
                if (!res.obj){
                    layer.msg("用户名不存在")
                }
            })
        }
    })

    // 实际使用时记得删除该代码
    layer.msg('欢迎来到WMS仓库管理系统', {
        offset: '15px'
        ,icon: 1
    });

});